﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2013-10-22
/// Purpose: 		应用的功能表
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2014-1-14       将验证是否匹配URL的正则表达式改为不区分大小写
/// Robin           2014-6-10       修改正则表达式匹配方式
/// ****************************************************************************
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RB.Domain.Models;
using System.Text.RegularExpressions;

namespace RB.Web.Security
{
    /// <summary>
    /// 应用的功能表
    /// </summary>
    [RB.DataAccess.Attributes.QueryName("Permission")]
    [RB.DataAccess.Attributes.TableName("Permission")]
    public class Permission : ICreateLog, IModifyLog
    {
        [System.ComponentModel.DataAnnotations.StringLengthAttribute(20)]
        public string Code { get; set; }

        [System.ComponentModel.DataAnnotations.StringLengthAttribute(20)]
        public string GroupCode { get; set; }

        [System.ComponentModel.DataAnnotations.StringLengthAttribute(100)]
        public string Name1 { get; set; }

        [System.ComponentModel.DataAnnotations.StringLengthAttribute(50)]
        public string CreateBy { get; set; }

        public DateTime? CreateTime { get; set; }

        [System.ComponentModel.DataAnnotations.StringLengthAttribute(50)]
        public string ModifyBy { get; set; }

        public DateTime? ModifyTime { get; set; }

        public bool IsDisabled { get; set; }

        public short Sequence { get; set; }

        /// <summary>
        /// 应用程序的代号
        /// </summary>
        [System.ComponentModel.DataAnnotations.StringLengthAttribute(20)]
        public string AppCode { get; set; }

        /// <summary>
        /// Url的字符串模式，用正则表达式表示
        /// </summary>
        [System.ComponentModel.DataAnnotations.StringLengthAttribute(255)]
        public string UriPattern { get; set; }

        /// <summary>
        /// Url参数的字符串模式，用正则表达式表示
        /// </summary>
        [System.ComponentModel.DataAnnotations.StringLengthAttribute(255)]
        public string UriParamPattern { get; set; }

        /// <summary>
        /// 此功能是否可以给非登录用户访问
        /// </summary>
        public bool IsPublic { get; set; }

        private Regex _pathRegex = null;
        private Regex[] _queryRegex = null;

        public bool IsMatch(string path, string query)
        {
            bool result = false;

            if (_pathRegex == null && !string.IsNullOrWhiteSpace(UriPattern))
            {
                _pathRegex = new Regex("^(/.+)*" + (!UriPattern.StartsWith("/") ? "/" : "") + UriPattern + "$", RegexOptions.IgnoreCase);
            }
            if (_pathRegex != null && _pathRegex.IsMatch(path))
            {
                if (_queryRegex == null && !string.IsNullOrWhiteSpace(UriParamPattern))
                {
                    _queryRegex = UriParamPattern.Split('&').Select(p => new Regex(p, RegexOptions.IgnoreCase)).ToArray();
                }
                if (_queryRegex != null)
                {
                    if (!_queryRegex.Any(p => !p.IsMatch(query)))
                    {
                        result = true;
                    }
                }
                else
                {
                    result = true;
                }
            }

            return result;
        }

    }


}
